package com.example.gamept;

import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

//实现接口 SurfaceHolder.Callback
public class GameUI extends SurfaceView implements SurfaceHolder.Callback {
    private boolean isRender;//标记是否要开启线程
    public GameUI(Context context) {
        super(context);

        //获取放映机
        SurfaceHolder holder = getHolder();
        //添加回调接口，这样才能在surfaceview中回调生命周期的api
        holder.addCallback(this);
    }


    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        System.out.println("SurfaceView 创建");
        //旗帜变为true
        isRender = true;
        RenderThread renderThread = new RenderThread();
        renderThread.start();//开启绘制线程
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        System.out.println("SurfaceView 修改");

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
        System.out.println("SurfaceView 销毁");
        //surfaceDestroyed后就要停止线程了
        isRender = false;

    }

    //绘制线程
    class RenderThread extends Thread{
        @Override
        public void run() {
            while (true){
                //写一个循环 不断地绘制屏幕
                drawUI();
            }
        }
    }

    //绘制屏幕
    private void drawUI(){
        System.out.println("开始绘制界面");
    }
}
